
package com.insigma.business.qrcode.soldier.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;


@Slf4j
public class PrivacyEncUtils {

    private static Map<Character, String> baseKvMap = null;
    private static Map<String, Character> reverseKvMap = null;
    private static String baseAesKey = "SYC1QAZ2WSX12345";

    static {
        baseKvMap = new HashMap<>();
        reverseKvMap = new HashMap<>();
        baseKvMap.put('0', "MA");
        baseKvMap.put('1', "MQ");
        baseKvMap.put('2', "Mg");
        baseKvMap.put('3', "Mw");
        baseKvMap.put('4', "NA");
        baseKvMap.put('5', "NQ");
        baseKvMap.put('6', "Ng");
        baseKvMap.put('7', "Nw");
        baseKvMap.put('8', "OA");
        baseKvMap.put('9', "OQ");
        baseKvMap.put('X', "eA");

        for (Map.Entry<Character, String> characterStringEntry : baseKvMap.entrySet()) {
            reverseKvMap.put(characterStringEntry.getValue(), characterStringEntry.getKey());
        }
    }


    private static final String REGEX_ID_CARD = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";


    public static String encodeIdCardNo(String idCardNo) {
        if (Strings.isBlank(idCardNo)) {
            return idCardNo;
        }
        if (!Pattern.matches(REGEX_ID_CARD, idCardNo)) {
            return idCardNo;
        }

        StringBuilder builder = new StringBuilder();
        idCardNo = idCardNo.toUpperCase();
        for (char c : idCardNo.toCharArray()) {
            String s = baseKvMap.get(c);
            BizCheckUtil.checkNotNull(s, "身份证错误");
            builder.append(s);
        }
        return builder.toString();
    }


    public static String encodeCharByIdCardNo(String idCardNoChar) {
        if (Strings.isBlank(idCardNoChar)) {
            return idCardNoChar;
        }
        if (idCardNoChar.length() > 28) {
            return idCardNoChar;
        }

        StringBuilder builder = new StringBuilder();
        idCardNoChar = idCardNoChar.toUpperCase();
        for (char c : idCardNoChar.toCharArray()) {
            String s = baseKvMap.get(c);
            BizCheckUtil.checkNotNull(s, "身份证错误");
            builder.append(s);
        }
        return builder.toString();
    }


    public static String decodeIdCardNo(String encodeIdCardNo) {
        if (Strings.isBlank(encodeIdCardNo)) {
            return encodeIdCardNo;
        }

        if (encodeIdCardNo.length() < 28) {
            return encodeIdCardNo;
        }

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < encodeIdCardNo.length(); i = i + 2) {
            String item = encodeIdCardNo.substring(i, i + 2);
            Character character = reverseKvMap.get(item);
            if (character == null) {
                log.error("不存在的身份证，{}", encodeIdCardNo);
            }
            BizCheckUtil.checkNotNull(character);
            builder.append(character);
        }
        return builder.toString();
    }


    public static String encodeMobile(String mobile) {
        if (Strings.isBlank(mobile)) {
            return mobile;
        }

        if (mobile.length() >= 16) {
            return mobile;
        }

        try {
            byte[] encrypt = AESUtil.encrypt(mobile.getBytes(), baseAesKey.getBytes());
            String s1 = Base64.encode(encrypt);
            return s1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String decodeMobile(String encodeMobile) {
        if (Strings.isBlank(encodeMobile)) {
            return encodeMobile;
        }

        if (encodeMobile.length() < 15) {
            return encodeMobile;
        }

        try {
            byte[] decode = Base64.decode(encodeMobile);
            byte[] encrypt = AESUtil.decrypt(decode, baseAesKey.getBytes());
            return new String(encrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String encodeCommonChinese(String str) {
        if (Strings.isBlank(str)) {
            return str;
        }

        if (str.matches(".*[\u4e00-\u9fa5].*?")) {
            return str;
        }

        try {
            byte[] encrypt = AESUtil.encrypt(str.getBytes(), baseAesKey.getBytes());
            String s1 = Base64.encode(encrypt);
            return s1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String decodeCommonChinese(String str) {
        if (Strings.isBlank(str)) {
            return str;
        }

        if (str.matches(".*[\u4e00-\u9fa5].*?")) {
            return str;
        }

        try {
            byte[] decode = Base64.decode(str);
            byte[] encrypt = AESUtil.decrypt(decode, baseAesKey.getBytes());
            return new String(encrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void transformExcel() {
        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\jin\\Desktop\\biao1.xls");
        final List<List<Object>> read = reader.read();
        for (int i = 1; i < read.size(); i++) {
            List<Object> objects = read.get(i);
            Object name = objects.get(0);
            Object gender = objects.get(1);
            Object idCardNo = objects.get(2);
            Object mobile = objects.get(3);
            Object address = objects.get(4);
            Object soldierType = objects.get(5);
            if ("MALE".equals(gender)) {
                objects.set(1, "男");
            }
            if ("FEMALE".equals(gender)) {
                objects.set(1, "女");
            }
            objects.set(2, decodeIdCardNo(idCardNo.toString()));
            if (mobile != null) {
                objects.set(3, decodeMobile(mobile.toString()));
            }

        }
        ExcelWriter writer = ExcelUtil.getWriter("C:\\Users\\jin\\Desktop\\biao1t.xls");
        writer.write(read);
    }


    public static String maskString(String input, int start, int offset, char maskChar) {
        if (input == null || start < 0) {
            // 处理输入不合法的情况
            throw new IllegalArgumentException("Invalid input or range");
        }

        // 将字符串转换为字符数组，以便修改中间的字符
        char[] chars = input.toCharArray();

        // 替换中间的字符为 *
        for (int i = start; i < input.length() && i <= start + offset; i++) {
            chars[i] = maskChar;
        }

        // 构建替换后的字符串
        return new String(chars);
    }

    public static void main(String[] args) {

        String s1 = encodeIdCardNo("330724197502281314");
        System.out.println(s1);


    }


}
